<template></template>

<script>
import Database from "./Database.vue";
let dbCollection = [];
export default {
  name: "DataQuery",
  methods: {
    async queryCollection(collectionName) {
      if (typeof collectionName !== "string") {
        return;
      }
      let item = dbCollection.find((item) => {
        return item.name === collectionName;
      });
      if (item != undefined) {
        console.log("DataQuery: 数据再一次获取成功");
        return item.data;
      }

      let db = Database.methods.Getdb();
      if (db == undefined) {
        let promise = Database.methods.init();
        await promise;
      }
      db = Database.methods.Getdb();
      if (db == undefined) {
        console.log("DataQuery: db 未定义");
        return;
      }
      const _ = db.command;
      let promise = db.collection(collectionName).get();
      let result = await promise;
      function retResult(res) {
        console.log("DataQuery: 数据查询成功");
        dbCollection.push({ name: collectionName, data: res.data });
        return res.data;
      }
      return retResult(result);
    },
  },
};
</script>